<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_dosen extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.nidn';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		$nama_dosen = isset($_POST['nama_dosen']) ? strval($_POST['nama_dosen']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_dosen, a.nidn, a.nama_dosen, b.nama_fakultas, c.nama_prodi, a.alamat, a.telepon, a.pendidikan_s1, a.pendidikan_s2, a.pendidikan_s3, a.status, a.pembantu');
			$this->db->from('ref_dosen a');
			$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.kd_fakultas');
			$this->db->join('ref_prodi c', 'c.kd_prodi = a.kd_prodi', 'left');
			
			#Kimochi - otoritas
				#--Faklutas
			if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('a.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('a.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			#End Kimochi - otoritas
			
			// if($kd_fakultas != ''){
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			// }
			// if($kd_prodi != ''){
				// $this->db->where('a.kd_prodi', $kd_prodi);
			// }
				if($kd_dosen != ''){
				$this->db->where('a.kd_dosen', $kd_dosen);
			}
			
			if($nama_dosen != ''){
				$this->db->like('nama_dosen', $nama_dosen, 'both');
			}
			$this->db->where('a.status', "A");
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ref_dosen a');
		$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.kd_fakultas');
		$this->db->join('ref_prodi c', 'c.kd_prodi = a.kd_prodi');
		$this->db->where('a.kd_dosen', $kode);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ref_dosen');
		
		return $this->db->get()->num_rows();
	}
	
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('kd_dosen',$kode);
		
		$this->db->select('*');
		$this->db->from('ref_dosen');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_dosen', $data['kd_dosen']);
		$this->db->set('nidn', $data['nidn']);
		$this->db->set('nama_dosen', $data['nama_dosen']);
		$this->db->set('kd_fakultas', $data['kd_fakultas']);
		$this->db->set('kd_prodi', $data['kd_prodi']);
		$this->db->set('pembantu', $data['pembantu']);
		$this->db->set('alamat', $data['alamat']);
		$this->db->set('telepon', $data['telepon']);
		$this->db->set('pendidikan_s1', $data['pendidikan_s1']);
		$this->db->set('pendidikan_s2', $data['pendidikan_s2']);		
		$this->db->set('pendidikan_s3', $data['pendidikan_s3']);		
		$this->db->set('status_dosen', $data['status_dosen']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('ref_dosen');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_dosen', $data['kd_dosen']);
		$this->db->set('nidn', $data['nidn']);
		$this->db->set('nama_dosen', $data['nama_dosen']);
		$this->db->set('kd_fakultas', $data['kd_fakultas']);
		$this->db->set('kd_prodi', $data['kd_prodi']);
		$this->db->set('pembantu', $data['pembantu']);
		$this->db->set('alamat', $data['alamat']);
		$this->db->set('telepon', $data['telepon']);
		$this->db->set('pendidikan_s1', $data['pendidikan_s1']);
		$this->db->set('pendidikan_s2', $data['pendidikan_s2']);		
		$this->db->set('pendidikan_s3', $data['pendidikan_s3']);		
		$this->db->set('status_dosen', $data['status_dosen']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_dosen', $data['kode']);
		$result = $this->db->update('ref_dosen');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_dosen', $kode);
		$result = $this->db->update('ref_dosen');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('*, b.nama_fakultas, c.nama_prodi', false);
		$this->db->from('ref_dosen a');
		$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.kd_fakultas');
		$this->db->join('ref_prodi c', 'c.kd_prodi = a.kd_prodi');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		//array_push($pdfdata['column'], array('caption'=>'kd_dosen', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'NIDN', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Dosen', 'width'=>40, 'align'=>'L', 'val_align'=>'L'));
		//array_push($pdfdata['column'], array('caption'=>'Fakultas', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Prodi', 'width'=>40, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Asisten Dosen', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Telepon', 'width'=>25, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Email', 'width'=>25, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Status', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				//$row->kd_dosen,
				$row->nidn,
				$row->nama_dosen,
				//$row->nama_fakultas,
				$row->nama_prodi,
				$row->pembantu,
				$row->alamat,
				$row->telepon,
				$row->email,
				$row->status_dosen
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	//PDF
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('a.kd_dosen, a.nidn, a.nama_dosen, b.nama_fakultas, c.nama_prodi, a.alamat, a.telepon, a.pendidikan_s1, a.pendidikan_s2, a.pendidikan_s3, a.status, a.pembantu, a.email');
		$this->db->from('ref_dosen a');
		$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.kd_fakultas');
		$this->db->join('ref_prodi c', 'c.kd_prodi = a.kd_prodi');
					
		$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			
			//$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
	public function importData($data){
		//query insert data
		$this->db->flush_cache();
		
		$this->db->set('kd_dosen',				$data['kd_dosen']);
		$this->db->set('nidn',					$data['nidn']);
		$this->db->set('nama_dosen',			$data['nama_dosen']);
		$this->db->set('kd_pt',					$data['kd_pt']);
		$this->db->set('kd_fakultas',			$data['kd_fakultas']);
		$this->db->set('kd_prodi',				$data['kd_prodi']);
		$this->db->set('alamat',				$data['alamat']);
		$this->db->set('telepon',				$data['telepon']);
		$this->db->set('email',					$data['email']);
		$this->db->set('pendidikan_s1',			$data['pendidikan_s1']);
		$this->db->set('pendidikan_s2',			$data['pendidikan_s2']);
		$this->db->set('pendidikan_s3',			$data['pendidikan_s3']);
		$this->db->set('status_dosen',			$data['status_dosen']);
		$this->db->set('status',				$data['status']);
		
		$result = $this->db->insert('ref_dosen');
		
		$errNo   = $this->db->_error_number();
	    $errMess = $this->db->_error_message();
		$error = $errMess;
		//var_dump($errMess);die;
	    log_message("error", "Problem import Inserting to : ".$errMess." (".$errNo.")"); 
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
}

?>